package com.zzk.lanouhn.myapplication1.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.MessageArticlesAdapter;
import com.zzk.lanouhn.myapplication1.intefaces.NewsItemCliclListener;
import com.zzk.lanouhn.myapplication1.modles.MessageArticles;
import com.zzk.lanouhn.myapplication1.utils.HttpUtils;
import com.zzk.lanouhn.myapplication1.view.ScrollRecyclerView;

import java.util.List;

/**
 * Created by lanouhn on 16/9/1.
 */
public class NewestDetailsActivity extends AppCompatActivity implements NewsItemCliclListener{

    private ImageView iv_back;
    private ScrollRecyclerView scrollRecyclerView;
    private MessageArticlesAdapter adapter;
    private List<MessageArticles.DataBean.TopicBean> articles;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newestdetails);
        initView();
        getDataFromWeb();
        initEvent();
    }

    private void initView() {
        iv_back = (ImageView) findViewById(R.id.iv_back_newest);
        scrollRecyclerView = (ScrollRecyclerView) findViewById(R.id.recycler_details_newest);
    }

    private void getDataFromWeb() {
        String articlesUrl = "http://open4.bantangapp.com/topics/topic/listByUsers?";
        String articlesParams = "app_id=com.jzyd.BanTang&client_id=bt_app_android" +
                "&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d" +
                "&track_user_id=2559956&oauth_token=6beffa8c92276d19d3c23acb596024e6" +
                "&track_deviceid=866962021116243&track_device_info=m1+note" +
                "&channel_name=meizu&app_installtime=1471923362&app_versions=5.8.7" +
                "&os_versions=5.1&screensize=1080&v=19&page=0&pagesize=20&sort_type=0";

        try {
            HttpUtils.doPostAsyn(articlesUrl, articlesParams, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    articles = gson.fromJson(result, MessageArticles.class).getData().getTopic();
                    if (articles.size() > 0) {
                        handler.sendEmptyMessage(1);

                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(NewestDetailsActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    adapter = new MessageArticlesAdapter(NewestDetailsActivity.this, articles);
                    adapter.setScrollRecyclerViewAdapter(NewestDetailsActivity.this);
                    scrollRecyclerView.setAdapter(adapter);
                    LinearLayoutManager manager = new LinearLayoutManager(NewestDetailsActivity.this);
                    manager.setOrientation(LinearLayoutManager.VERTICAL);
                    scrollRecyclerView.setLayoutManager(manager);
                    break;
                default:

                    break;
            }
        }
    };

    private void initEvent(){
       iv_back.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               finish();
           }
       });
    }

    @Override
    public void onItemClick(int position) {
        Intent intent = new Intent(NewestDetailsActivity.this, ArticlesListDetailsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("内容", articles.get(position).getId());
        intent.putExtras(bundle);
        startActivity(intent);
    }
}
